package com.xbb.controller.sys;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xbb.common.RespResult;
import com.xbb.entity.Notice;
import com.xbb.entity.Question;
import com.xbb.entity.Questionnaire;
import com.xbb.mapper.QuestionMapper;
import com.xbb.mapper.QuestionnaireMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sys/questionnaire")
public class SysQuestionnaireController {

    private String prefix = "/sys/questionnaire/";

    @Resource
    private QuestionnaireMapper questionnaireMapper;
    @Resource
    private QuestionMapper questionMapper;

    // 问卷管理
    @RequestMapping("/manage.html")
    public String manageHtml() {
        return prefix + "questionnaire";
    }

    // 分页数据
    @ResponseBody
    @RequestMapping("/data")
    public RespResult data (@RequestParam("page") int page,
                            @RequestParam("limit") int limit,
                            @RequestParam(value = "title", required = false, defaultValue = "") String title,
                            @RequestParam(value = "status", required = false) Integer status) {
        if (title.equals("")) title = null;
        List<Questionnaire> questionnaireList = questionnaireMapper.selectListByPaging((page - 1) * limit, limit, title, status);
        int count = questionnaireMapper.selectCountByPaging(title, status);
        RespResult respResult = new RespResult();
        respResult.success(questionnaireList, count);
        return respResult;
    }

    // 添加
    @ResponseBody
    @RequestMapping("/add")
    public RespResult add(@RequestBody Questionnaire questionnaire) {
        questionnaireMapper.insertSelective(questionnaire);
        return new RespResult();
    }

    // 编辑
    @RequestMapping("/edit.html")
    public String editHtml (@RequestParam("id") int id, Model model) {
        Questionnaire questionnaire = questionnaireMapper.selectByPrimaryKey(id);
        List<Question> questionList = questionMapper.selectListByQuestionnaireId(id);
        model.addAttribute("questionnaire", questionnaire);
        model.addAttribute("questionList", questionList);
        return prefix + "edit";
    }

    @ResponseBody
    @RequestMapping("/edit")
    public RespResult edit(@RequestBody Questionnaire questionnaire) {
        if (questionnaire.getStatus() == 1) {
            questionnaire.setCreatTime(new Date());
        }
        questionnaireMapper.updateByPrimaryKeySelective(questionnaire);
        return new RespResult();
    }

    // 详情
    @RequestMapping("/item.html")
    public String itemHtml (@RequestParam("id") int id, Model model) {
        Questionnaire questionnaire = questionnaireMapper.selectByPrimaryKey(id);
        List<Question> questionList = questionMapper.selectListByQuestionnaireId(id);
        model.addAttribute("questionnaire", questionnaire);
        model.addAttribute("questionList", questionList);
        return prefix + "item";
    }

    // 删除
    @ResponseBody
    @RequestMapping("/delete")
    public RespResult delete(@RequestBody Integer id) {
        questionnaireMapper.deleteByPrimaryKey(id);
        return new RespResult();
    }

}
